{% extends "base.html" %}
{% block head %}<meta http-equiv="refresh" content="5;url={{ redirect }}">{% endblock %}
{% block content %}
<h1>Voted successfully</h1>
<p>Thanks for contributing. You will be redirected in 5 secondes to <a href="{{ redirect }}">poll summary</a></p>
{% endblock %}

{% block code %}
<span class="k">class</span> <span class="nc">DoVote</span><span class="p">(</span><span class="n">View</span><span class="p">):</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s">&quot;polls/poll_vote.html&quot;</span>
    <span class="n">login_required</span> <span class="o">=</span> <span class="bp">True</span>
    
    <span class="k">def</span> <span class="nf">build_params</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">poll_id</span><span class="p">,</span> <span class="n">choice_id</span><span class="p">):</span>
        <span class="n">user</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span>
        <span class="n">poll</span> <span class="o">=</span> <span class="n">get_object_or_404</span><span class="p">(</span><span class="n">Poll</span><span class="p">,</span> <span class="n">pk</span><span class="o">=</span><span class="n">poll_id</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">user</span><span class="o">.</span><span class="n">is_authenticated</span><span class="p">()</span> <span class="ow">and</span> <span class="n">Vote</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">poll</span><span class="o">=</span><span class="n">poll</span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">user</span><span class="o">=</span><span class="n">user</span><span class="p">):</span>
            <span class="k">raise</span> <span class="n">CutResponse</span><span class="p">(</span><span class="n">HttpResponse</span><span class="p">(</span><span class="s">&#39;already voted&#39;</span><span class="p">))</span>
        <span class="n">choice</span> <span class="o">=</span> <span class="n">get_object_or_404</span><span class="p">(</span><span class="n">Choice</span><span class="p">,</span> <span class="n">pk</span><span class="o">=</span><span class="n">choice_id</span><span class="p">)</span>
        <span class="n">vote</span> <span class="o">=</span> <span class="n">Vote</span><span class="p">(</span><span class="n">user</span><span class="o">=</span><span class="n">user</span><span class="p">,</span> <span class="n">poll</span><span class="o">=</span><span class="n">poll</span><span class="p">,</span> <span class="n">choice</span><span class="o">=</span><span class="n">choice</span><span class="p">,</span>
                    <span class="n">ip_address</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">META</span><span class="p">[</span><span class="s">&#39;REMOTE_ADDR&#39;</span><span class="p">])</span>
        <span class="n">vote</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
        <span class="k">return</span> <span class="p">{</span><span class="s">&#39;redirect&#39;</span><span class="p">:</span> <span class="n">poll</span><span class="o">.</span><span class="n">get_absolute_url</span><span class="p">()}</span>
{% endblock %}
